package raccoon.plugin.mybatis;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.PreparedStatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ConfigurationStealer;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * Created by Administrator on 2016/6/30.
 */
public class BatchPreparedStatementHandler extends PreparedStatementHandler{

    protected final ParameterHandler parameterHandler;

    public BatchPreparedStatementHandler(Executor executor, MappedStatement mappedStatement, List<Object> parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        super(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);

        ParameterHandler parameterHandler = new BatchParameterHandler(mappedStatement,parameterObject,boundSql);
        parameterHandler = (ParameterHandler) ConfigurationStealer.getInterceptorChain(configuration).pluginAll(parameterHandler);
        this.parameterHandler = parameterHandler;
    }

    @Override
    public ParameterHandler getParameterHandler() {
        return parameterHandler;
    }

    @Override
    public void parameterize(Statement statement) throws SQLException {
        parameterHandler.setParameters((PreparedStatement) statement);
    }
}
